package server;

import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class ClientHandler implements Runnable{
	public Thread thread;
	private int port;
	private ServerSocket serverSocket;
	private ObjectInputStream in;
	public ClientHandler(int port){
		thread = new Thread(this);
		this.port = port;
		thread.start();
	}
	
	
	
	public void run(){
		try{
			serverSocket = new ServerSocket(port);
			serverSocket.setSoTimeout(2000); 
			System.out.println("Waiting for client to connect...");
			Socket client = serverSocket.accept(); //connecttion to a new client
			in = new ObjectInputStream(client.getInputStream());
			Object obj= in.readObject();
			if(obj instanceof String){
				String playerName = (String)obj;
				new Player(playerName, client);
			}
			in.close();
		}
		catch(SocketException ei){
			System.out.println("Client connection timed out..");
		}
		catch(Exception e){
			System.out.println("problems in -ClientHandler-");
		}
		
	}

}
